package com.ants.platform.core.db.mybatis;

import org.apache.ibatis.session.*;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by liushun on 2016/7/16.
 */
@Repository
public class MyBatisDAOImpl implements MyBatisDAO {

    @Resource(name = "sqlSessionTemplate")
    private SqlSessionTemplate sqlSessionTemplate = null;


    /**
     * 查询一条记录 不带参数
     *
     * @param xmlStr xml配置
     * @param <T>    返回实体
     * @return
     */
    public <T> T selectOne(String xmlStr) {
        return sqlSessionTemplate.selectOne(xmlStr);
    }

    /**
     * 查询一条记录 带参数
     *
     * @param xmlStr xml配置
     * @param params 条件参数
     * @param <T>
     * @return
     */
    public <T> T selectOne(String xmlStr, Object params) {
        return sqlSessionTemplate.selectOne(xmlStr, params);
    }

    public <E> List<E> selectList(String xmlStr) {
        return sqlSessionTemplate.selectList(xmlStr);
    }

    public <E> List<E> selectList(String xmlStr, Object params) {
        return sqlSessionTemplate.selectList(xmlStr, params);
    }

    public <E> List<E> selectList(String xmlStr, Object params, RowBounds rowBounds) {
        return sqlSessionTemplate.selectList(xmlStr, params, rowBounds);
    }

    public <K, V> Map<K, V> selectMap(String xmlStr, String params) {
        return sqlSessionTemplate.selectMap(xmlStr, params);
    }

    public <K, V> Map<K, V> selectMap(String xmlStr, Object params, String str) {
        return sqlSessionTemplate.selectMap(xmlStr, params, str);
    }

    public <K, V> Map<K, V> selectMap(String xmlStr, Object params, String str, RowBounds rowBounds) {
        return sqlSessionTemplate.selectMap(xmlStr, params, str, rowBounds);
    }

    public void select(String xmlStr, Object params, ResultHandler resultHandler) {
        sqlSessionTemplate.select(xmlStr, params, resultHandler);
    }

    public void select(String xmlStr, ResultHandler resultHandler) {
        sqlSessionTemplate.select(xmlStr, resultHandler);
    }

    public void select(String xmlStr, Object params, RowBounds rowBounds, ResultHandler resultHandler) {
        sqlSessionTemplate.select(xmlStr, params, rowBounds, resultHandler);
    }

    public int insert(String xmlStr) {
        return sqlSessionTemplate.insert(xmlStr);
    }

    public int insert(String xmlStr, Object params) {
        return sqlSessionTemplate.insert(xmlStr, params);
    }

    public int update(String xmlStr) {
        return sqlSessionTemplate.update(xmlStr);
    }

    public int update(String xmlStr, Object params) {
        return sqlSessionTemplate.delete(xmlStr, params);
    }

    public int delete(String xmlStr) {
        return sqlSessionTemplate.delete(xmlStr);
    }

    public int delete(String xmlStr, Object params) {
        return sqlSessionTemplate.delete(xmlStr, params);
    }


    public void batchUpdate(String xmlStr, List params) {
        SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();
        //批量执行器
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            if (params != null) {
                for (int i = 0, size = params.size(); i < size; i++) {
                    sqlSession.update(xmlStr, params.get(i));
                }
                sqlSession.flushStatements();
                sqlSession.commit();
                sqlSession.clearCache();
            }
        } finally {
            sqlSession.close();
        }
    }

}
